package com.lr.students.controller;

import com.lr.students.entry.Result;
import com.lr.students.entry.Code;
import com.lr.students.model.Classess;
import com.lr.students.model.Students;
import com.lr.students.model.StudentsInfo;
import com.lr.students.service.StudentsService;
import com.github.pagehelper.PageInfo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;



@RestController
@RequestMapping("/students")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentsService studentsService;



    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){

        PageInfo<StudentsInfo> pageInfo = studentsService.findPage(page, size);
        return new Result<PageInfo>(true, Code.OK,"查询成功",pageInfo);
    }


    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){

        studentsService.delete(id);
        return new Result(true, Code.OK,"删除成功");
    }



    @GetMapping
    public Result<List<Students>> findAll(){

        List<StudentsInfo> list = studentsService.findAll();
        return new Result<List<Students>>(true, Code.OK,"查询成功",list) ;
    }

    @GetMapping("/classess")
    public Result<List<Classess>> findclassessAll(){

        List<Classess> list = studentsService.findclassessAll();
        return new Result<List<Classess>>(true, Code.OK,"查询成功",list) ;
    }
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Students students,@PathVariable String id){

        students.setStudentId(id);

        studentsService.update(students);
        return new Result(true, Code.OK,"修改成功");
    }

    @PostMapping
    public Result add(@RequestBody   Students students){

        studentsService.add(students);
        return new Result(true, Code.OK,"添加成功");
    }


    @GetMapping("/{id}")
    public Result<Students> findById(@PathVariable String id){

        Students students = studentsService.findById(id);
        return new Result<Students>(true, Code.OK,"查询成功",students);
    }


}
